﻿namespace Microsoft.SharePoint.Client.Workflow
{
    using Microsoft.SharePoint.Client;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.Workflow.WorkflowTemplateCollection", ServerTypeId="{f64cc3d2-888a-43cc-b579-ef32f83338a4}")]
    public class WorkflowTemplateCollection : ClientObjectCollection<WorkflowTemplate>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public WorkflowTemplateCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public WorkflowTemplate GetById(Guid templateId)
        {
            Dictionary<Guid, WorkflowTemplate> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetById", out obj2))
            {
                dictionary = (Dictionary<Guid, WorkflowTemplate>) obj2;
            }
            else
            {
                dictionary = new Dictionary<Guid, WorkflowTemplate>();
                base.ObjectData.MethodReturnObjects["GetById"] = dictionary;
            }
            WorkflowTemplate template = null;
            if (!dictionary.TryGetValue(templateId, out template))
            {
                template = new WorkflowTemplate(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { templateId }));
                dictionary[templateId] = template;
            }
            return template;
        }

        [Remote]
        public WorkflowTemplate GetByName(string name)
        {
            Dictionary<string, WorkflowTemplate> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient && (name == null))
            {
                throw ClientUtility.CreateArgumentNullException("name");
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByName", out obj2))
            {
                dictionary = (Dictionary<string, WorkflowTemplate>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, WorkflowTemplate>();
                base.ObjectData.MethodReturnObjects["GetByName"] = dictionary;
            }
            WorkflowTemplate template = null;
            if (!dictionary.TryGetValue(name, out template))
            {
                template = new WorkflowTemplate(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByName", new object[] { name }));
                dictionary[name] = template;
                ObjectIdentityQuery query = new ObjectIdentityQuery(template.Path);
                base.Context.AddQueryIdAndResultObject(query.Id, template);
                base.Context.AddQuery(query);
            }
            return template;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

